<?php

namespace Biz\model\wiki;
use Biz\model\entity\SpuRelateWiki as SpuRelateWikiEntity;
use Doraemon\tools\Log;

class SpuRelateWiki extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'spu_relate_wiki';

    const STATUS_WAIT = 0;
    const STATUS_RELATE_SUCCESS = 1;
    const STATUS_RELATE_FAIL = 2;
    const STATUS_RELATE_FAIL_REPEAT = 3;//重复数据错误
    const STATUS_RELATE_FAIL_EXISTS = 4;//已经存在
    const STATUS_RELATE_FAIL_INFO_EMPTY = 5;//商品信息查询失败
    const STATUS_RELATE_FAIL_INVALID = 6;//参数无效
    const STATUS_DELETE = 9;

    public static function failRelateWiki($goods_id, $fail_status = self::STATUS_RELATE_FAIL)
    {
        $condition = [
            'goods_id'  => $goods_id,
            'relate_status[!]' => self::STATUS_DELETE,
        ];
        $failData = [
            'relate_status' => $fail_status,
            'edittime'      => date("Y-m-d H:i:s"),
        ];
        return self::update($failData, $condition)->rowCount();
    }

    public static function relateWiki($goods_id, $wiki_id)
    {
        $condition = [
            'goods_id'  => $goods_id,
            'relate_status[!]' => self::STATUS_DELETE,
        ];
        $relateData = [
            'wiki_id'       => $wiki_id,
            'relate_status' => self::STATUS_RELATE_SUCCESS,
            'edittime'      => date("Y-m-d H:i:s"),
        ];
        return self::update($relateData, $condition)->rowCount();
    }

    public static function coverData(SpuRelateWikiEntity $entity)
    {
        $condition = [
            'goods_id'  => $entity->goods_id,
            'relate_status[!]' => self::STATUS_DELETE,
        ];
        $coverData = [
            'transform_platform'    => $entity->transform_platform,
            'transform_shop_id'     => $entity->transform_shop_id,
            'transform_shop_name'   => $entity->transform_shop_name,
            'transform_shop_spu'    => $entity->transform_shop_spu,
            'transform_brand'       => $entity->transform_brand,
            'relate_status'        => self::STATUS_WAIT,
            'admin_uid'     => $entity->admin_uid,
            'edittime'      => date("Y-m-d H:i:s"),
        ];
        self::update($coverData, $condition);
    }

    public static function insertData(SpuRelateWikiEntity $entity)
    {
        $data = [
            'goods_id'              => $entity->goods_id,
            'wiki_id'               => 0,
            'transform_platform'    => $entity->transform_platform,
            'transform_shop_id'     => $entity->transform_shop_id,
            'transform_shop_name'   => $entity->transform_shop_name,
            'transform_shop_spu'    => $entity->transform_shop_spu,
            'transform_brand'       => $entity->transform_brand,
            'shop_id'               => $entity->shop_id,
            'admin_uid'             => $entity->admin_uid,
            'relate_status'         => self::STATUS_WAIT,
            'addtime'               => date("Y-m-d H:i:s"),
        ];
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return self::id();
    }
    /**
     * 检查wiki关联的spu
     * @param $wiki_id
     * @return array
     */
    public static function getWikiExists($wiki_id)
    {
        $condition = [
            'wiki_id'  => $wiki_id,
            'relate_status[!]' => self::STATUS_DELETE,
        ];
        return self::get('goods_id', $condition);
    }

    /**
     * 查询spu存在转换任务的商品id
     * @param $goods_id
     * @return array
     */
    public static function checkExists($goods_id)
    {
        $condition = [
            'goods_id'  => $goods_id,
            'relate_status[!]' => self::STATUS_DELETE,
        ];
        return self::select('goods_id', $condition);
    }

    public static function getWikiByGoodsIds($goods_id)
    {
        $condition = [
            'wiki_id[>]'  => 0,
            'goods_id'  => $goods_id,
            'relate_status[!]' => self::STATUS_DELETE,
        ];
        return self::select(['goods_id','wiki_id'], $condition);
    }
}